//菜单切换
$('#tag li').click(function () {
    $(this).addClass("selected").siblings().removeClass("selected");
});

//项目类别选择
$('.option span').click(function () {
    $(this).addClass("selected").siblings().removeClass("selected");
    $('button').click();
});

//日历
//$(function () {
//    $.datepicker.regional['zh-CN'] = {
//        changeYear: true,
//        monthNamesShort: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
//        dayNamesMin: ['日', '一', '二', '三', '四', '五', '六']
//    };
//    $.datepicker.setDefaults($.datepicker.regional['zh-CN']);
//    $("#defult").datetimepicker();
//    $("#start").prop("readonly", true).datepicker({
//        changeMonth: true,
//        dateFormat: "yy-mm-dd",
//    });
//    $("#end").prop("readonly", true).datepicker({
//        changeMonth: true,
//        dateFormat: "yy-mm-dd",
//    });
//    $("#start2").prop("readonly", true).datepicker({
//        changeMonth: true,
//        dateFormat: "yy-mm-dd",
//    });
//    $("#end2").prop("readonly", true).datepicker({
//        changeMonth: true,
//        dateFormat: "yy-mm-dd",
//    });
//});

//中标结果样式
$('.if').each(function () {
    if ($(this).text() == "已定标") {
        $(this).css('color', '#1e9fff');
    }
});

//点击删除
$('.delete').click(function () {
//    confirm("确定要删除此记录？");
});


//格式化时间
//$('body').find('[dateformat]').each(function () {
//    var when = $(this).attr('dateformat');
//    $(this).text(new Date(parseInt(when) * 1000).toLocaleString().split(' ')[0].replace(/年|月/g, "-").replace(/日/g, " ").replace(/\//g, "-"));
//});
//分页
var numb;
var firstpage;
var lastpage;
var nextpage;
var lastpage;
var shijian="";
var enddt="";
var txt;
var stor=1;
var page=0;
var fengzi = $('#page a b').first();
var fengmu = $('#page a b').last();
var start = $('#start').val();
var end =$('#end').val();
query(1,1,start,end);
num(1,start,end);
//点击查询按钮
$('#button').click(function(){
 start = $('#start').val();
 end =$('#end').val();
 if(start!=""){
 start = new Date(start).getTime()/1000;
 }
 if(end!=""){
     end = new Date(end).getTime()/1000;
 }
 fengzi.text("1");
 $('[name=jump]').val("");
 query(1,stor,start,end);
 num(stor,start,end);
});
//选择分类
    $('#tag li').click(function(){
       $('#start').val("");
       $('#end').val("");
       $('[name=jump]').val("");
       $('#participation').empty();
       stor = $(this).attr("stor");
       shijian="";
       enddt="";
       query(1,stor,shijian,enddt);
       num(stor,shijian,enddt);
       fenlei(stor);
    });
//首页
    function first(){
        query(1,1,start,end);
        fengzi.text("1");
    }
//上一页    
    function previous(){
        var zi= parseInt(fengzi.text());
        var mu = parseInt(fengmu.text());
        if(zi>1){
        zi= zi-1;
        fengzi.text(zi);
        query(zi,stor,start,end);
        }
    }
//下一页    
    function next(){
        var zi= parseInt(fengzi.text());
        var mu = parseInt(fengmu.text());
        if(zi<mu){
        zi= zi+1;
        fengzi.text(zi);
        query(zi,stor,start,end);
        }
    }
//尾页    
    function last(){
        query(fengmu.text(),1,start,end);
        fengzi.text(fengmu.text());
    }
//跳转
  function jump(){
   var jump = $('[name=jump]').val();
   var mu = parseInt(fengmu.text());
   if(1<=jump&&jump<=mu){
        fengzi.text(jump);
        query(jump,stor,start,end);
   }
  }    
//默认查询
    function query(page,stor,shijian,enddt){
    $('#participation').empty();
    var purchase_class = $("#option").find("span.selected").attr("purchase_class");
    txt="";
    new Ajax("/my/source_query.jsp").keyvalue({"page":page,"stor":stor,"shijian":shijian,"enddt":enddt,"purchase_class":purchase_class}).getJson(function(json){
     $.each(json, function(key, val){
     shijian = new Date(val["shijian"] * 1000).toString().substring(0,10);
     if(val["enddt"]=="2769091200"){
         enddt="长期有效"
     }else{
         enddt= new Date(val["enddt"]*1000).toString().substring(0,10);
     }
     if(stor==1){
     txt += "<tr><td><a href='finding_details.jsp?id="+val["id"]+"'>"+val["name"]+"</a></td><td>"+shijian+"</td><td>"+enddt+"</td></tr>";
 }else{
      txt += "<tr><td><a href='my_sourec.jsp?id="+val["id"]+"'>"+val["name"]+"</a></td><td>"+shijian+"</td><td>"+enddt+"</td></tr>";
 }
     })
     $('#participation').html(txt);
 }) 
    }
    function num(stor,shijian,enddt){
       var purchase_class = $("#option").find("span.selected").attr("purchase_class");
        new Ajax("/my/source_page.jsp").keyvalue({"stor":stor,"shijian":shijian,"enddt":enddt,"purchase_class":purchase_class}).get(function(data){
        numb = Math.ceil(data.replace(/[^0-9]/ig, "")/10);
        fengmu.text(numb);
    });
     fengzi.text("1");
    }
    //赋予分类
    function fenlei(stor){
        $('#first').attr("stor",stor);
        $('#previous').attr("stor",stor);
        $('#next').attr("stor",stor);
        $('#last').attr("stor",stor);
        $('#jump').attr("stor",stor);
    }